package com.shmn.gongjiajumain.service.impl;

import com.shmn.gongjiajumain.entity.ProductionLineEntity;
import com.shmn.gongjiajumain.entity.UserEntity;
import com.shmn.gongjiajumain.service.ProductionLineService;
import com.shmn.gongjiajumain.service.UserService;
import com.shmn.gongjiajumain.utils.DateUtil;
import com.shmn.gongjiajumain.vo.purchaseincome.Jig;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseIncomeTableVo;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseincomesubmithistoryFromVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.Query;

import com.shmn.gongjiajumain.dao.PurchaseIncomeSubmitDao;
import com.shmn.gongjiajumain.entity.PurchaseIncomeSubmitEntity;
import com.shmn.gongjiajumain.service.PurchaseIncomeSubmitService;

import javax.annotation.Resource;


@Service("purchaseIncomeSubmitService")
public class PurchaseIncomeSubmitServiceImpl extends ServiceImpl<PurchaseIncomeSubmitDao, PurchaseIncomeSubmitEntity> implements PurchaseIncomeSubmitService {

    @Resource
    private UserService userService;

    @Resource
    private ProductionLineService productionLineService;


    @Override
    public void saveInfo(PurchaseIncomeSubmitEntity purchaseIncomeSubmit) {
        List<Jig> jigs = purchaseIncomeSubmit.getJigs();
        String code = "";
        String count = "";
        for (int i = 0; i < jigs.size(); i++) {
            Jig jig = jigs.get(i);
            code += jig.getCode();
            count += jig.getCount();
            if (i == jigs.size() - 1) break;
            code += "|";
            count += "|";
        }
        purchaseIncomeSubmit.setCode(code);
        purchaseIncomeSubmit.setCount(count);
        purchaseIncomeSubmit.setSubmitTime(new Date());
        System.out.println(purchaseIncomeSubmit);
        baseMapper.insert(purchaseIncomeSubmit);
    }

    @Override
    public PageUtils queryPage1(PurchaseincomesubmithistoryFromVo purchaseincomesubmithistoryFromVo) {
        /**
         * 条件查询+分页
         */
        Map<String, Object> params = new HashMap<>();
        params.put("page",purchaseincomesubmithistoryFromVo.getPageIndex());
        params.put("limit",purchaseincomesubmithistoryFromVo.getPageSize());
        params.put("key",purchaseincomesubmithistoryFromVo.getKey());

        QueryWrapper<PurchaseIncomeSubmitEntity> wrapper = new QueryWrapper<>();
        if (purchaseincomesubmithistoryFromVo.getCode() != null){
            wrapper.like("code",purchaseincomesubmithistoryFromVo.getCode());
        }
        if (purchaseincomesubmithistoryFromVo.getBillNo() != null){
            wrapper.like("bill_no",purchaseincomesubmithistoryFromVo.getBillNo());
        }
        if (purchaseincomesubmithistoryFromVo.getStatus() != null){
            wrapper.eq("status",purchaseincomesubmithistoryFromVo.getStatus());
        }
        if (purchaseincomesubmithistoryFromVo.getStartToEndTime() != null){
            List<String> startToEndTime = purchaseincomesubmithistoryFromVo.getStartToEndTime();
            String startTime = startToEndTime.get(0);
            String endTime = startToEndTime.get(1);
            if (startTime != null && endTime != null){
                wrapper.between("submit_time",startTime,endTime);
            }else if (startTime != null){
                wrapper.ge("submit_time",startTime);
            }else {
                wrapper.le("submit_time",endTime);
            }
        }
        IPage<PurchaseIncomeSubmitEntity> page = this.page(
                new Query<PurchaseIncomeSubmitEntity>().getPage(params),
                wrapper
        );

        /**
         * 构建并返回vo对象
         */
        PageUtils pageUtils = new PageUtils(page);
        List<PurchaseIncomeTableVo> purchaseIncomeTableVos = new ArrayList<>();
        Class<PurchaseIncomeSubmitEntity> clazz = PurchaseIncomeSubmitEntity.class;
        pageUtils.getList().forEach(item->{
            PurchaseIncomeTableVo purchaseIncomeTableVo = new PurchaseIncomeTableVo();
            BeanUtils.copyProperties(item,purchaseIncomeTableVo);
            String submitId = null;
            Integer productionLineId = null;
            String code = null;
            String count = null;
            Date submitTime = null;
            try {
                Field field1 = clazz.getDeclaredField("submitId");
                Field field2 = clazz.getDeclaredField("productionLineId");
                Field field3 = clazz.getDeclaredField("code");
                Field field4 = clazz.getDeclaredField("count");
                Field field5 = clazz.getDeclaredField("submitTime");
                field1.setAccessible(true);
                field2.setAccessible(true);
                field3.setAccessible(true);
                field4.setAccessible(true);
                field5.setAccessible(true);
                submitId = (String)field1.get(item);
                productionLineId =(Integer) field2.get(item);
                code = (String) field3.get(item);
                count = (String) field4.get(item);
                submitTime = (Date) field5.get(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String codeAndCount = getCodeAndCount(code, count);
            UserEntity user = userService.getById(submitId);
            ProductionLineEntity productionLine = productionLineService.getById(productionLineId);
            purchaseIncomeTableVo.setSubmitTime(DateUtil.formatDate(submitTime));
            purchaseIncomeTableVo.setSubmitName(user.getName());
            purchaseIncomeTableVo.setProductionLine(productionLine.getWorkcell());
            purchaseIncomeTableVo.setCodeAndCount(codeAndCount);
            purchaseIncomeTableVos.add(purchaseIncomeTableVo);
        });
        pageUtils.setList(purchaseIncomeTableVos);
        return pageUtils;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseIncomeSubmitEntity> page = this.page(
                new Query<PurchaseIncomeSubmitEntity>().getPage(params),
                new QueryWrapper<PurchaseIncomeSubmitEntity>()
        );

        PageUtils pageUtils = new PageUtils(page);
        List<PurchaseIncomeTableVo> purchaseIncomeTableVos = new ArrayList<>();
        Class<PurchaseIncomeSubmitEntity> clazz = PurchaseIncomeSubmitEntity.class;
        pageUtils.getList().forEach(item->{
            PurchaseIncomeTableVo purchaseIncomeTableVo = new PurchaseIncomeTableVo();
            BeanUtils.copyProperties(item,purchaseIncomeTableVo);
            String submitId = null;
            Integer productionLineId = null;
            String code = null;
            String count = null;
            Date submitTime = null;
            try {
                Field field1 = clazz.getDeclaredField("submitId");
                Field field2 = clazz.getDeclaredField("productionLineId");
                Field field3 = clazz.getDeclaredField("code");
                Field field4 = clazz.getDeclaredField("count");
                Field field5 = clazz.getDeclaredField("submitTime");
                field1.setAccessible(true);
                field2.setAccessible(true);
                field3.setAccessible(true);
                field4.setAccessible(true);
                field5.setAccessible(true);
                submitId = (String)field1.get(item);
                productionLineId =(Integer) field2.get(item);
                code = (String) field3.get(item);
                count = (String) field4.get(item);
                submitTime = (Date) field5.get(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String codeAndCount = getCodeAndCount(code, count);
            UserEntity user = userService.getById(submitId);
            ProductionLineEntity productionLine = productionLineService.getById(productionLineId);
            purchaseIncomeTableVo.setSubmitTime(DateUtil.formatDate(submitTime));
            purchaseIncomeTableVo.setSubmitName(user.getName());
            purchaseIncomeTableVo.setProductionLine(productionLine.getWorkcell());
            purchaseIncomeTableVo.setCodeAndCount(codeAndCount);
            purchaseIncomeTableVos.add(purchaseIncomeTableVo);
        });
        pageUtils.setList(purchaseIncomeTableVos);
        return pageUtils;
    }

    private String getCodeAndCount(String code,String count){
        if (code == null || count == null) return null;
        String[] codes = code.split("\\|");
        String[] counts = count.split("\\|");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < codes.length; i++) {
            builder.append(codes[i] + " - " + counts[i] + "件");
            if (i == codes.length - 1) break;
            builder.append(" | ");
        }
        return builder.toString();
    }
}